package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.BusinessTrip;
import com.amsp.oa.oademo.service.BusinessTripService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/business-trip")
@Api(tags = "公务外出接口")
public class BusinessTripController {

    @Autowired
    private BusinessTripService businessTripService;

    @PostMapping("/save")
    @ApiOperation("新增公务外出申请")
    public ResponseEntity<BusinessTrip> save(@RequestBody BusinessTrip businessTrip) {
        if (businessTrip.getId() != null) {
            throw new RuntimeException("新增时不应该包含ID");
        }
        businessTripService.save(businessTrip);
        return ResponseEntity.ok(businessTrip);
    }

    @PutMapping("/update")
    @ApiOperation("更新公务外出申请")
    public ResponseEntity<BusinessTrip> update(@RequestBody BusinessTrip businessTrip) {
        if (businessTrip.getId() == null) {
            throw new RuntimeException("更新时ID不能为空");
        }
        businessTripService.updateById(businessTrip);
        return ResponseEntity.ok(businessTrip);
    }

    @GetMapping("/list")
    @ApiOperation("获取公务外出列表")
    public ResponseEntity<IPage<BusinessTrip>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String department) {
        return ResponseEntity.ok(businessTripService.getPage(current, size, name, department));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取公务外出详情")
    public ResponseEntity<BusinessTrip> getById(@PathVariable Long id) {
        return ResponseEntity.ok(businessTripService.getById(id));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除公务外出申请")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        businessTripService.removeById(id);
        return ResponseEntity.ok().build();
    }
}